//index.js
//获取应用实例
const app = getApp();

Page({
  data: {
    circular: true,
    current: '',
    slideShowList: [],
    coursesList: [],
    midTextList: [], 
    school_enterpiseList: [],
    circular: true,

    imgUrls: [],
    ListImg: [{}],
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    coursesInfoList: [],
    midTextInfoList: [],
    Hei: "", //这是swiper要动态设置的高度属性
    imgh:''
  },
  onLoad: function(options) {
    var me = this;

    //请求首页的各个模块的背景图
    var serverUrl = app.serverUrl;
    wx.request({
      url: serverUrl + '/getIndexImage',
      method: "Post",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          var imageInfo = res.data.data;
          var array = me.data.coursesList;
          var slideShowList = imageInfo.slideshow.split('|');
          var coursesList = imageInfo.courses.split('|');
          var coursesImageList = imageInfo.coursesimage.split('|');
          var midTextList = imageInfo.midtext.split('|');
          var school_enterpiseList = imageInfo.schoolEnterpise.split('|');

          for (var i = 0; i < slideShowList.length; i++) {
            slideShowList[i] = serverUrl + slideShowList[i];
            console.log(slideShowList[i]);
          }
        }
        for (var i = 0; i < midTextList.length; i++) {
          midTextList[i] = serverUrl + midTextList[i];
          console.log(midTextList[i]);
        }
        for (var i = 0; i < school_enterpiseList.length; i++) {
          school_enterpiseList[i] = serverUrl + school_enterpiseList[i];
          console.log(school_enterpiseList[i]);
        }
        var newarray;
        for (var i = 0; i < coursesList.length; i++) {
          newarray = [{
            img: serverUrl + coursesImageList[i],
            text: coursesList[i]
          }]
          array = array.concat(newarray);
        }
        me.setData({
          slideShowList: slideShowList,
          coursesList: array,
          midTextList: midTextList,
          school_enterpiseList: school_enterpiseList,
        })
      }

    })


    //请求课程内容列表
    setTimeout(function() {
      var serverUrl = app.serverUrl;
      wx.request({
        url: serverUrl + '/getClassInfo',
        method: "Post",
        success: function(res) {
          console.log(res.data)
          var imageInfo = res.data.data;
          var coursesInfoList = [];
          console.log(coursesInfoList)
          if (res.data.status == 200) {

            for (var i = 0; i < imageInfo.length; i++) {
              coursesInfoList[i] = serverUrl + imageInfo[i].classinfo
              console.log(coursesInfoList[i])
            }
            console.log(coursesInfoList);
            me.setData({
              coursesInfoList: coursesInfoList
            });

          }

        }
      })
    }, 1000)

    //请求中考体育内容列表
    setTimeout(function() {
      var serverUrl = app.serverUrl;
      wx.request({
        url: serverUrl + '/getMidText',
        data: {
          condition: 'three'
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function(res) {
          console.log(res.data);

          var imageInfo = res.data.data;
          var midTextInfoList = imageInfo[0].classinfo.split('|');

          console.log(midTextInfoList)
          if (res.data.status == 200) {

            for (var i = 0; i < midTextInfoList.length; i++) {
              midTextInfoList[i] = serverUrl + midTextInfoList[i]
              console.log(midTextInfoList[i])
            }
            console.log(midTextInfoList);
            me.setData({
              midTextInfoList: midTextInfoList
            });

          }

        }
      })
    }, 2000)


  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  //用户下拉刷新事件
  onPullDownRefresh() {
    wx.startPullDownRefresh()
  },

  //轮播图自适应宽高
  imgH: function(e) {
    //获取当前屏幕的宽度
    var winWid = wx.getSystemInfoSync().windowWidth;
    //图片高度
    var imgh = e.detail.height;　　　　　　　　　　　　　　　　
    var imgw = e.detail.width;
    //等比设置swiper的高度。  即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    var swiperH = winWid * imgh / imgw + "px"　
    this.setData({
      //设置高度
      Hei: swiperH,
      imgh: 　swiperH　　　
    })
  },

  //课程内容根据下标的不同进行不同内容的显示
  showClassInfo: function(e) {
    var me = this;
    var coursesInfoList = me.data.coursesInfoList;
    //点击项的下标
    var arrindex = e.target.dataset.arrindex;
    console.log('课程介绍下标' + arrindex);
    //json对象不能在页面跳转时传递需要转换为字符串
    var coursesInfo = JSON.stringify(coursesInfoList[arrindex]);
    console.log('课程详情' + coursesInfo);
    if (arrindex == '2') {
      wx.showToast({
        image:'/images/icon_loading.png'
      })
    } else {
      wx.navigateTo({
        url: '../../pages/detail/detail?coursesInfo=' + coursesInfo,
      })
    }

  },
  //中考部分根据图片下标不同显示对应的内容
  bindGrade: function(e) {
    var me = this;
    var midTextInfoList = me.data.midTextInfoList;
    //点击项的data-id
    var midid = e.currentTarget.dataset.id;
    console.log(e.currentTarget.dataset.id); //打印出data-id绑定的id值
    //json对象不能在页面跳转时传递需要转换为字符串
    var midTextInfo = JSON.stringify(midTextInfoList[midid]);
    console.log('中考背景' + midTextInfo);

    wx.navigateTo({
      url: '../../pages/grade/grade?midTextInfo=' + midTextInfo
    })
  },


  //跳转到预约
  toReservation: function() {
    wx.navigateTo({
      url: '../reservation/reservation'
    })
  },
  bindViewTap: function() {
    wx.navigateTo({
      url: '../../pages/contact/contact'
    })
  },
  toTeacher: function() {
    wx.navigateTo({
      url: '../../pages/teacher/index'
    })
  },
  toStyle: function() {
    wx.navigateTo({
      url: '../../pages/style/index'
    })
  },
  bindPosition: function() {
    wx.navigateTo({
      url: '../../pages/position/position'
    })
  },
  bindSchool: function() {
    wx.navigateTo({
      url: '../../pages/school/school'
    })
  },
  bindactivity: function() {
    wx.navigateTo({
      url: '../../pages/activity/activity'
    })
  },
  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
    }
    return {
      title: '转发',
      path: '/pages/index/index',
      success: function (res) {
        console.log('成功', res)
      }
    }
  },
})